'use strict';
(function (exports) {
    var OutCards = cc.Layer.extendEX({
        ctor: function () {
            this._super();
            this.removeAllChildren();
            return this;
        },
        setPos: function (pos) {
            this.pos = pos;
            mAction.outCards[this.pos] = [];
        },
        addCard: function (cardNo, animation, showCard) {
            var smallSize = mCard.getSmall2Size();
            var cardSize = mCard.getCardSize();
            var cardList = mAction.outCards[this.pos];
            cardList.push(cardNo);
            var card = new Card();
            this.addChild(card);
            card.setData(cardNo, null, 3);
            var x = 5;
            if (this.pos == 0)
                x = 5;
            if (ZiPaiRoomData.playerNum == 4) {
                x = 4;
            }
            if (ZiPaiRoomData.playerNum == 2)
                x = 9;
            if (gameData.mapId == app.map_id.QYLDS){
                if (ZiPaiRoomData.playerNum == 2) x = 14;
                else x = 12;
            }
            if (gameData.mapId == app.map_id.YZCHZ){
                x = 12;
            }

            var rowIndex = parseInt((cardList.length - 1) / x);
            var colIndex = (cardList.length - 1) % x;
            var pos = null;
            var frompos = null;
            var posH = 360;//cardSize.h*3+5;
            var curContentSize = this.getContentSize();
            if (gameData.mapId == app.map_id.QYLDS || gameData.mapId == app.map_id.YZCHZ){
                if (ZiPaiRoomData.playerNum == 4) {
                    if (this.pos == 0) {
                        pos = cc.p(colIndex * smallSize.w, rowIndex * smallSize.h);
                        frompos = cc.p(DesignSize.width / 2, 450);
                    } else if (this.pos == 1) {
                        pos = cc.p(-colIndex * smallSize.w, rowIndex * smallSize.h);
                        frompos = cc.p(DesignSize.width - 260, 270);
                    } else if (this.pos == 2) {
                        pos = cc.p(-colIndex * smallSize.w, rowIndex * smallSize.h);
                        frompos = cc.p(DesignSize.width - 260, DesignSize.height - 270);
                    } else {
                        pos = cc.p(colIndex * smallSize.w, rowIndex * smallSize.h);
                        frompos = cc.p(260, DesignSize.height - 270);
                    }
                }
                else if (ZiPaiRoomData.playerNum == 3) {
                    if (this.pos == 0) {
                        pos = cc.p(colIndex * smallSize.w, rowIndex * smallSize.h);
                        frompos = cc.p(DesignSize.width / 2, 450);
                    } else if (this.pos == 1) {
                        pos = cc.p(-colIndex * smallSize.w, rowIndex * smallSize.h);
                        frompos = cc.p(DesignSize.width - 260, DesignSize.height - 270);
                    } else if (this.pos == 2) {
                        pos = cc.p(colIndex * smallSize.w, rowIndex * smallSize.h);
                        frompos = cc.p(260, DesignSize.height - 270);
                    }
                }
                else if (ZiPaiRoomData.playerNum == 2) {
                    if (this.pos == 0) {
                        pos = cc.p(colIndex * smallSize.w, rowIndex * smallSize.h);
                        frompos = cc.p(DesignSize.width / 2, 450);
                    } else if (this.pos == 1) {
                        pos = cc.p(colIndex * smallSize.w, rowIndex * smallSize.h);
                        frompos = cc.p(260, DesignSize.height - 270);
                    }
                }
            }
            else if (ZiPaiRoomData.playerNum < 4) {
                if (this.pos == 1) {
                    pos = cc.p(- (colIndex * smallSize.w+ smallSize.w/2), rowIndex * smallSize.h+ smallSize.h/2);
                    frompos = cc.p(cc.winSize.width / 2 + 270 - smallSize.w / 2, posH);
                } else if (this.pos == 2) {
                    pos = cc.p(colIndex * smallSize.w+ smallSize.w/2, rowIndex * smallSize.h+ smallSize.h/2);
                    frompos = cc.p(cc.winSize.width / 2 - 270 - smallSize.w / 2, posH);
                } else if (this.pos == 0) {
                    pos = cc.p(-(colIndex * smallSize.w + smallSize.w/2), rowIndex * smallSize.h+ smallSize.h/2);
                    frompos = cc.p(cc.winSize.width / 2 - smallSize.w / 2, posH);
                }
            }
            else {
                if (this.pos == 1) {
                    pos = cc.p(145 - colIndex * smallSize.w, rowIndex * smallSize.h);
                    frompos = cc.p(780, 320);
                } else if (this.pos == 2) {
                    pos = cc.p(145 - colIndex * smallSize.w, rowIndex * smallSize.h);
                    frompos = cc.p(260, 320);
                } else if (this.pos == 0) {
                    pos = cc.p(colIndex * smallSize.w + 25, rowIndex * smallSize.h);
                    frompos = cc.p(510, 320);
                } else {
                    pos = cc.p(colIndex * smallSize.w + 25, rowIndex * smallSize.h);
                    frompos = cc.p(510, 320);
                }
            }
            card.setPosition(pos);
            var worldpos = card.convertToWorldSpace(cc.p(0, 0));
            card.setPosition(cc.p(pos.x - (worldpos.x - frompos.x), pos.y - (worldpos.y - frompos.y)));
            card.setVisible(false);
            if (animation) {
                // if (showCard) {
                //     showCard.runAction(cc.sequence(
                //         cc.delayTime(0.6)
                //         , cc.scaleTo(0.1, 0.01)
                //     ));
                // }

                if (showCard) {
                    var dupShowCard = new Card();
                    dupShowCard.setData(showCard.data, null, 1);
                    this.addChild(dupShowCard);
                    dupShowCard.setPosition(cc.p(pos.x - (worldpos.x - frompos.x), pos.y - (worldpos.y - frompos.y)));
                    var removeDupShowCardX = function (){
                        dupShowCard.removeFromParent();
                    };
                    dupShowCard.runAction(cc.sequence(
                        cc.moveTo(0.2, pos.x, pos.y),
                        cc.callFunc(removeDupShowCardX)
                    ));
                    dupShowCard.runAction(cc.sequence(
                        cc.delayTime(0.5),
                        cc.fadeOut(0.25),
                        cc.delayTime(0.5),
                        cc.fadeOut(0.25)
                    ));
                    dupShowCard.runAction(cc.sequence(
                        cc.scaleTo(0.3, 0.1)
                    ));
                }

                var showCardX = function (){
                    card.setVisible(true);
                    card.setPosition(cc.p(pos.x, pos.y));
                };
                card.runAction(cc.sequence(
                    cc.delayTime(0.3),
                    cc.callFunc(showCardX)
                ));
            } else {
                card.setVisible(true);
                card.setPosition(pos);
            }
            card.setOutCardray();
        },
        resetCard: function () {
            var cardList = this.getChildren();
            for (var i = 0; i < cardList.length; i++) {
                var card = cardList[i];
                card.setData(cardList[i], null, 2);
            }
        },

    });
    exports.OutCards = OutCards;
})(window);